package Cliente.interface_grafico;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.EventObject;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;

import mensagem.MIdJogador;
import mensagem.TipoMensagem;

public class BotaoPerfilEditor extends DefaultCellEditor {
	private ObjectOutputStream output;
	private int id_jogador;
	private JButton b_verperfil;
	private boolean isPushed = false;

	public BotaoPerfilEditor(JCheckBox checkBox, ObjectOutputStream out) {
		super(checkBox);
		assert (output != null);
		this.output = out;
		id_jogador = -1;
		b_verperfil = new JButton();
		b_verperfil.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireEditingStopped();
			}
		});
	}

	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		if (isSelected) {
			b_verperfil.setForeground(table.getSelectionForeground());
			b_verperfil.setBackground(table.getSelectionBackground());
		} else{
			b_verperfil.setForeground(table.getForeground());
			b_verperfil.setBackground(table.getBackground());
		}
		b_verperfil.setText("ver perfil");
		id_jogador = (Integer)value;
		isPushed = true;
		return b_verperfil;
	}

	public Object getCellEditorValue() {
		if (isPushed) {
			//Aqui vem a codigo que deve ser executado quando se carrega no botao.
			try {
				synchronized(output){
				output.writeObject(new MIdJogador(TipoMensagem.CLIENTE_VERPERFIL, (Integer)id_jogador));
				output.flush();
				}
			} catch (IOException e) {
			}
		}
		isPushed = false;
		return id_jogador ;
	}

	public boolean stopCellEditing() {
		isPushed = false;
		return super.stopCellEditing();
	}

	protected void fireEditingStopped() {
		super.fireEditingStopped();
	}
}
